package com.test.dababy.dababy_00_analytics.dababydata_sdk;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababylogger.DababyASLger;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcAppsTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcBtyTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcClogTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcCntaTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcHadwTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcSsTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcStorTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASDevcWfiTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyASFilterOdsTsk;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyOnDevcTskCmple;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababytask.DababyOnFilterOdsTskCmpt;
import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyutil.DababyASUtil;
import com.test.dababy.dababy_00_utils.DababyUtil;

import java.util.concurrent.CompletableFuture;

public class DababyASSycManager {


    public int fyaA=175;
    public int ukY4=134;
    public int zBj7=18;
    public double r593=0.2818243757421336;
    public boolean c1Vb=true;
    public char yOhq='Í';
    public String ue7A="";

    public void k7nXd(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(ue7A))
                    setUe7A(String.valueOf(getFyaA())+getUkY4()+getzBj7()+getR593()+isC1Vb()+getyOhq());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }


    private final static String TAG = "ASSycManager";

    private DababyASSycManager() {
    }

    @SuppressLint("StaticFieldLeak")
    private static DababyASSycManager instance_dababy;

    public void setFyaA(int fyaA) {
        this.fyaA = fyaA;
    }

    public void setUkY4(int ukY4) {
        this.ukY4 = ukY4;
    }

    public void setzBj7(int zBj7) {
        this.zBj7 = zBj7;
    }

    public void setR593(double r593) {
        this.r593 = r593;
    }

    public void setC1Vb(boolean c1Vb) {
        this.c1Vb = c1Vb;
    }

    public void setyOhq(char yOhq) {
        this.yOhq = yOhq;
    }

    public void setUe7A(String ue7A) {
        this.ue7A = ue7A;
    }

    public static DababyASSycManager getInstance_dababy() {
        if (instance_dababy == null) {
            synchronized (DababyASSycManager.class) {
                instance_dababy = new DababyASSycManager();
            }
        }
        return instance_dababy;
    }

    public Context mContext_dababy;

    private DababyOnDevcTskCmple mOnDevcTskCmpledababy_dababy;

    public void init_dababy(Context applicationContext) {
        mContext_dababy = applicationContext;
    }

    public void sycData_dababy(DababyOnDevcTskCmple completeListener) {
        try {
            this.mOnDevcTskCmpledababy_dababy = completeListener;

            DababyASDevcSsTsk dababy = new DababyASDevcSsTsk(mContext_dababy, DababyASBuilder.T_MAG);

            k7nXd();

            // 创建任务列表
            CompletableFuture<String> device1Task = CompletableFuture.supplyAsync(new DababyASDevcBtyTsk(mContext_dababy, DababyASBuilder.T_BTY));
            CompletableFuture<String> device2Task = CompletableFuture.supplyAsync(new DababyASDevcStorTsk(mContext_dababy, DababyASBuilder.T_STO));
            CompletableFuture<String> device3Task = CompletableFuture.supplyAsync(new DababyASDevcAppsTsk(mContext_dababy, DababyASBuilder.T_APL));
            CompletableFuture<String> device4Task = CompletableFuture.supplyAsync(new DababyASDevcWfiTsk(mContext_dababy, DababyASBuilder.T_NTW));
            CompletableFuture<String> device5Task = CompletableFuture.supplyAsync(new DababyASDevcHadwTsk(mContext_dababy, DababyASBuilder.T_HDW));
            CompletableFuture<String> device6Task = CompletableFuture.supplyAsync(new DababyASDevcTsk(mContext_dababy, DababyASBuilder.T_DIE));
            CompletableFuture<String> device7Task = CompletableFuture.supplyAsync(dababy);
            CompletableFuture<String> device8Task = CompletableFuture.supplyAsync(new DababyASDevcCntaTsk(mContext_dababy, DababyASBuilder.T_COTACT));
            CompletableFuture<String> device9Task = CompletableFuture.supplyAsync(new DababyASDevcClogTsk(mContext_dababy, DababyASBuilder.T_CAL));

            // 提交任务
            CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(
                    device1Task,
                    device2Task,
                    device3Task,
                    device4Task,
                    device5Task,
                    device6Task,
                    device7Task,
                    device8Task,
                    device9Task);

            // 在所有任务触发后回调，不一定要等结果完成！！！
            allOfFuture.thenRun(() -> {
                // 日记标识
                DababyASLger.d_dababy(TAG, "sycData: 在所有任务触发后回调");
                DababyUtil.safeRun(() -> {
                    if (mOnDevcTskCmpledababy_dababy != null) {
                        mOnDevcTskCmpledababy_dababy.onAllCompleted_dababy();
                    }
                });

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void filterOrders_dababy(DababyOnFilterOdsTskCmpt completeListener) {
        CompletableFuture<String> filterOrdersTask = CompletableFuture.supplyAsync(new DababyASFilterOdsTsk(mContext_dababy));
        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(filterOrdersTask);
        allOfFuture.thenRunAsync(() -> {
            String result = DababyASUtil.safeString_dababy(filterOrdersTask.join()).trim();

            // 日记标识
            DababyASLger.d_dababy(TAG, "filterOrders: 在所有任务完成后回调");
            DababyASLger.d_dababy(TAG, "filterOrders result: " + result);

            // 主线程回调
            DababyUtil.safeRun(() -> {
                if (completeListener != null) {
                    completeListener.onAllCompleted_dababy(result);
                }
            });
        });
    }

    public static void setInstance_dababy(DababyASSycManager instance_dababy) {
        DababyASSycManager.instance_dababy = instance_dababy;
    }

    public void setmContext_dababy(Context mContext_dababy) {
        this.mContext_dababy = mContext_dababy;
    }

    public void setmOnDevcTskCmpledababy_dababy(DababyOnDevcTskCmple mOnDevcTskCmpledababy_dababy) {
        this.mOnDevcTskCmpledababy_dababy = mOnDevcTskCmpledababy_dababy;
    }

    public int getFyaA() {
        return fyaA;
    }

    public int getUkY4() {
        return ukY4;
    }

    public int getzBj7() {
        return zBj7;
    }

    public double getR593() {
        return r593;
    }

    public boolean isC1Vb() {
        return c1Vb;
    }

    public char getyOhq() {
        return yOhq;
    }

    public String getUe7A() {
        return ue7A;
    }
}
